package com.sskj.b70.v.activity;

import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.github.yoojia.inputs.AndroidNextInputs;
import com.github.yoojia.inputs.StaticScheme;
import com.sskj.b70.MyAppLication;
import com.sskj.b70.R;
import com.sskj.b70.common.App;
import com.sskj.b70.common.widget.ToolBarLayout;
import com.sskj.b70.p.activity.ApplyAgentPersonPresenter;
import com.sskj.b70.utils.ClickUtil;
import com.sskj.b70.v.base.BaseActivity;

import butterknife.BindView;

/**
 * Created by lv on 18-6-11.
 */

public class ApplyAgentPersonActivity extends BaseActivity<ApplyAgentPersonPresenter> {

    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.tv_code_get)
    TextView tvCodeGet;
    @BindView(R.id.bt_submit)
    Button btSubmit;
    @BindView(R.id.tv_jigou)
    TextView tvJigou;
    @BindView(R.id.agent_toolbar)
    ToolBarLayout agentToolbar;
    private CountDownTimer countDownTimer;

    @Override
    protected void initView() {
        agentToolbar.setLeftButtonOnClickLinster(v -> finish());
        tvJigou.setText(MyAppLication.getJGCode());
        ClickUtil.click(tvCodeGet, () -> {
            tvCodeGet.setEnabled(false);
            mPresenter.sendCode();
        });
        AndroidNextInputs input = new AndroidNextInputs()
                .add(etCode, StaticScheme.Required());
        ClickUtil.click(btSubmit, () -> {
            String code = etCode.getText().toString().trim();

            if (input.test()) {
                mPresenter.applyAgent(code);
            }
        });
    }


    public static void start(Context context) {
        Intent starter = new Intent(context, ApplyAgentPersonActivity.class);
        context.startActivity(starter);

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_apply_agent_person;
    }

    @Override
    public ApplyAgentPersonPresenter getPresenter() {
        return new ApplyAgentPersonPresenter();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
    }

    public void onSendSmsSuccess() {
        countDownTimer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                tvCodeGet.setText(millisUntilFinished / 1000 + App.INSTANCE.getString(R.string.strApplyAgentPersonActivity0));
            }

            @Override
            public void onFinish() {
                tvCodeGet.setEnabled(true);
                tvCodeGet.setText(App.INSTANCE.getString(R.string.strApplyAgentPersonActivity1));
            }
        }.start();
    }
}
